﻿#include "GroupBox_SetSerialPort.h"

#include <qserialportinfo.h>

GroupBox_SetSerialPort::GroupBox_SetSerialPort(QWidget* parent)
  : QGroupBox(parent)
{
  ui.setupUi(this);
}

GroupBox_SetSerialPort::~GroupBox_SetSerialPort()
{
}

void GroupBox_SetSerialPort::setUpSerialPort(QSerialPort* qSerialPort)
{
  qSerialPort->setPortName(getPortName());
  qSerialPort->setBaudRate(getBaudRate());
  qSerialPort->setParity(getParity());
  qSerialPort->setDataBits(getDataBits());
  qSerialPort->setStopBits(getStopBits());
  qSerialPort->setFlowControl(getFlowControl());
}

void GroupBox_SetSerialPort::showSerialPortInfo(QSerialPort* qSerialPort)
{
  updatePortName(qSerialPort->portName());
  updateBaudRate(qSerialPort->baudRate());
  updateParity(qSerialPort->parity());
  updateDataBits(qSerialPort->dataBits());
  updateStopBits(qSerialPort->stopBits());
  updateFlowControl(qSerialPort->flowControl());
}

QString GroupBox_SetSerialPort::getPortName()
{
  return ui.comboBox_portName->currentText();
}

void GroupBox_SetSerialPort::updatePortName(QString portName)
{
  QList<QSerialPortInfo> portList = QSerialPortInfo::availablePorts();
  ui.comboBox_portName->clear();
  for (int i=0;i<portList.size();++i)
    {
      ui.comboBox_portName->addItem(portList.at(i).portName());
      if (portList.at(i).portName().toLower()== portName.toLower())
        {
          ui.comboBox_portName->setCurrentIndex(i);
        }
    }
}
QSerialPort::Parity GroupBox_SetSerialPort::getParity()
{
  switch (ui.comboBox_parityBit->currentIndex())//设置校验位
    {
    case 0:
      return QSerialPort::NoParity;
      break;
    case 1:
      return QSerialPort::EvenParity;
      break;
    case 2:
      return QSerialPort::OddParity;
      break;
    default:
      return QSerialPort::NoParity;
    }
}
void GroupBox_SetSerialPort::updateParity(QSerialPort::Parity parity)
{
  switch (parity)
    {
    case QSerialPort::NoParity:
      ui.comboBox_parityBit->setCurrentIndex(0);
      break;
    case QSerialPort::EvenParity:
      ui.comboBox_parityBit->setCurrentIndex(1);
      break;
    case QSerialPort::OddParity:
      ui.comboBox_parityBit->setCurrentIndex(2);
      break;
    default:
      break;
    }
}
QSerialPort::DataBits GroupBox_SetSerialPort::getDataBits()
{
  switch (ui.comboBox_dataBit->currentIndex())//设置数据位
    {
    case 0:
      return QSerialPort::Data5;
      break;
    case 1:
      return QSerialPort::Data6;
      break;
    case 2:
      return QSerialPort::Data7;
      break;
    case 3:
      return QSerialPort::Data8;
      break;
    default:
      return QSerialPort::Data5;
    }
}
void GroupBox_SetSerialPort::updateDataBits(QSerialPort::DataBits dataBits)
{
  switch (dataBits)
    {
    case QSerialPort::Data5:
      ui.comboBox_dataBit->setCurrentIndex(0);
      break;
    case QSerialPort::Data6:
      ui.comboBox_dataBit->setCurrentIndex(1);
      break;
    case QSerialPort::Data7:
      ui.comboBox_dataBit->setCurrentIndex(2);
      break;
    case QSerialPort::Data8:
      ui.comboBox_dataBit->setCurrentIndex(3);
      break;
    default:
      break;
    }
}
int GroupBox_SetSerialPort::getBaudRate()
{
  return ui.comboBox_baudRate->currentText().toInt();
}
void GroupBox_SetSerialPort::updateBaudRate(int baudRate)
{
  switch (baudRate)
    {
    case 1200:
      ui.comboBox_baudRate->setCurrentIndex(0);
      break;
    case 2400:
      ui.comboBox_baudRate->setCurrentIndex(1);
      break;
    case 4800:
      ui.comboBox_baudRate->setCurrentIndex(2);
      break;
    case 9600:
      ui.comboBox_baudRate->setCurrentIndex(3);
      break;
    case 19200:
      ui.comboBox_baudRate->setCurrentIndex(4);
      break;
    case 38400:
      ui.comboBox_baudRate->setCurrentIndex(5);
      break;
    case 57600:
      ui.comboBox_baudRate->setCurrentIndex(6);
      break;
    case 115200:
      ui.comboBox_baudRate->setCurrentIndex(7);
      break;
    case 460800:
      ui.comboBox_baudRate->setCurrentIndex(8);
      break;
    default:
      ui.comboBox_baudRate->setCurrentIndex(0);
      break;
    }
}
QSerialPort::StopBits GroupBox_SetSerialPort::getStopBits()
{
  switch (ui.comboBox_stopBit->currentIndex())//设置停止位
    {
    case 0:
      return QSerialPort::OneStop;
      break;
    case 1:
      return QSerialPort::OneAndHalfStop;
      break;
    case 2:
      return QSerialPort::TwoStop;
      break;
    default:
      return QSerialPort::OneStop;
    }
}
void GroupBox_SetSerialPort::updateStopBits(QSerialPort::StopBits stopBits)
{
  switch (stopBits)
    {
    case QSerialPort::OneStop:
      ui.comboBox_stopBit->setCurrentIndex(0);
      break;
    case QSerialPort::OneAndHalfStop:
      ui.comboBox_stopBit->setCurrentIndex(1);
      break;
    case QSerialPort::TwoStop:
      ui.comboBox_stopBit->setCurrentIndex(2);
      break;
    default:
      break;
    }
}
QSerialPort::FlowControl GroupBox_SetSerialPort::getFlowControl()
{
  switch (ui.comboBox_flowControl->currentIndex())//设置流控制
    {
    case 0:
      return QSerialPort::NoFlowControl;
      break;
    case 1:
      return QSerialPort::HardwareControl;
      break;
    case 2:
      return QSerialPort::SoftwareControl;
      break;
    default:
      return QSerialPort::NoFlowControl;
    }
}
void GroupBox_SetSerialPort::updateFlowControl(QSerialPort::FlowControl flowControl)
{
  switch (flowControl)
    {
    case QSerialPort::NoFlowControl:
      ui.comboBox_flowControl->setCurrentIndex(0);
      break;
    case QSerialPort::HardwareControl:
      ui.comboBox_flowControl->setCurrentIndex(1);
      break;
    case QSerialPort::SoftwareControl:
      ui.comboBox_flowControl->setCurrentIndex(2);
      break;
    default:
      break;
    }
}
